package main

import (
	"fmt"
)

type Phone struct {
	Name string
}

type Camera struct {
	Name string
}
type Usb interface {
	start()
	stop()
}

func (this Phone) start() {
	fmt.Println(this.Name, "start()")
}
func (this Phone) stop() {
	fmt.Println(this.Name, "stop()")
}

func (this Camera) start() {
	fmt.Println(this.Name, "start()")
}
func (this Camera) stop() {
	fmt.Println(this.Name, "stop()")
}

type Computer struct {
}

func (this Computer) Working(usb Usb) {
	usb.start()
	if Phone, ok := usb.(Phone); ok {
		Phone.Call()
	}
	usb.stop()
}

func (this Phone) Call() {
	fmt.Println(this.Name, "铃声响了")
}

//类型断言

func main() {
	//多态数组
	var usbArr [2]Usb
	usbArr[0] = Phone{Name: "iphone"}
	usbArr[1] = Camera{Name: "sony"}
	var computer Computer = Computer{}
	for _, v := range usbArr {
		computer.Working(v) //Phone 和 Camera 都实现了Usb接口，可以作为参数
	}
}
